package org.particles.domain.field;

import org.particles.domain.LinearFunction;
import org.particles.domain.Particle;
import org.particles.domain.Vector;

/**
 * uses per default force strength of 10 and LinearFunction as distanceFunc with
 * factor 100. use this force for electron particle simulation.
 */
public class RejectionForce extends AbstractField {
	
	/**
	 * uses per default force strength of 10 and LinearFunction as distanceFunc with
	 * factor 100.
	 */
	public RejectionForce(final long fieldMarker) {
		super(fieldMarker);
		setStrength(10);
		setDistanceFunc(new LinearFunction(100));
	}

	@Override
	public void applyForces(final Particle p1, final Particle p2) {
		final double factor;
		final FieldParams params = p1.getFieldParams(getFieldMarker());
		if(params == null || params.distToZero == null) {
			factor = getDistanceFunc().calc(p1.distanceTo(p2));
		}
		else {
			factor = getDistanceFunc().calc(p1.distanceTo(p2), params.distToZero);
		}
		final double forceStrengthForParticle;
		if(params == null || params.fieldStrength == null) {
			forceStrengthForParticle = getStrength() * factor;
		}
		else {
			forceStrengthForParticle = params.fieldStrength * factor;
		}
		final Vector forceVector = p1.getSpacePlace().subtractFrom(p2.getSpacePlace());
		forceVector.setLength(forceStrengthForParticle);
		p2.getImpulse().add(forceVector);
	}

}
